<?php

// +----------------------------------------------------------------------
// | 版权所有 2017-2027 云南环拓科技有限公司，并保留所有权利。
// +----------------------------------------------------------------------
// | Copyright (c) 2017-2027 http://www.yn123.com All rights reserved.
// +----------------------------------------------------------------------
// | Notice: This code is not open source, it is strictly prohibited
// |         to distribute the copy, otherwise it will pursue its
// |         legal responsibility.
// | 注意：  这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用,
// |        不允许对程序代码以任何形式任何目的的再发布。
// +----------------------------------------------------------------------
// | Author: feraner <feraner@163.com> 
// | Date: 2019/12/24
// +----------------------------------------------------------------------

namespace app\command;

use app\common\facade\Arr;
use app\common\services\wechattemplate\WechatPayTemplateService;
use think\console\Input;
use think\console\Output;
use think\Db;

/**
 * 该脚本每2个小时执行一次
 * Class RemindPayCommand
 * @package app\command
 * @author feraner<feraner@163.com>
 * @date 2019/12/24
 */
class RemindPayCommand extends BasicCommand
{

    protected $name = 'pay:remind-pay';

    protected $desc = '提醒用户支付订单程序';


    /**
     * @param Input  $input
     * @param Output $output
     *
     * @return int|null|void
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function execute(Input $input, Output $output)
    {


        //提醒用户支付订单
        $this->__remindPay();


    }


    /**
     * 提醒用户支付未付款的订单
     * @throws \WeChat\Exceptions\InvalidResponseException
     * @throws \WeChat\Exceptions\LocalCacheException
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function __remindPay()
    {
        $limit = [
            'offset'        => 0,
            'length'        => 20
        ];
        while (true) {
            $noPayList = $this->__getNotPayList($limit);
            if ($noPayList) {
                foreach ($noPayList as $item) {
                    //提醒用户支付
                    (new WechatPayTemplateService())->remindPayMoney($item);
                }
                $limit['offset'] += $limit['length'];
            } else {
                break;
            }
            sleep(2);
        }
    }


    /**
     * 获取未支付需要提醒的充值订单
     * @param $limit
     *
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    private function __getNotPayList($limit)
    {
        $chargeList = Db::name('user_charge')
            ->where('status', 0)
            ->where('money', '<>', 0)
            ->where('finished_time', 0)
            ->limit(Arr::get($limit, 'offset'), Arr::get($limit, 'length'))
            ->select() ?: [];
        return $chargeList;
    }








}
